﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GlxRRSDB.ReportDataSetTableAdapters;
using GlxRRSDB;
using GlxRRSMain.Utils;

namespace GlxRRSMain.UserControls
{
    public partial class ImportCODataControl : UserControl
    {
        private const int COLUMN_CINEMA_IDX = 0;
        private const int COLUMN_DATE_IDX = 3;
        private const int COLUMN_CO_IDX = 5;
        //
        private tblCORevenueTableAdapter x_tblCORevenueTableAdapter = new tblCORevenueTableAdapter();
        System.Data.DataSet DtSet;
        public ImportCODataControl()
        {
            InitializeComponent();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            DataTable dtTable = DtSet.Tables[0];
            int cachedCinemaID = 0;
            DateTime cachedDate = new DateTime();
            //
            for (int i = 1; i < dtTable.Rows.Count; i++)
            {
                // read cinema id/ cinema name
                string cinemaName = dtTable.Rows[i][COLUMN_CINEMA_IDX].ToString();
                if (cinemaName.ToLower().Contains("nguyen du"))
                {
                    cachedCinemaID = 1001;
                }
                else if (cinemaName.ToLower().Contains("nguyen trai"))
                {
                    cachedCinemaID = 1002;
                }
                else if (cinemaName.ToLower().Contains("tan binh"))
                {
                    cachedCinemaID = 1003;
                }
                // read date
                string strDate = dtTable.Rows[i][COLUMN_DATE_IDX].ToString();
                //if (strDate == "") continue;
                if (strDate.ToLower().Contains("total")) continue;
                if (!"".Equals(strDate))
                {
                    cachedDate = DateTime.Parse(strDate);
                }
                //DateTime date = DateTime.Parse(strDate);
                // read co
                string strCO = dtTable.Rows[i][COLUMN_CO_IDX].ToString();
                if (strCO == "") continue;
                float co = float.Parse(strCO);
                // insert data
                x_tblCORevenueTableAdapter.Insert(cachedCinemaID, (long)co, cachedDate);
            }
            MessageBox.Show("Imported total:" + dtTable.Rows.Count + " rows");
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            importFileDialog.Filter = "Excel (.xlsx)|*.xlsx|All Files (*.*)|*.*";
            importFileDialog.FilterIndex = 1;
            importFileDialog.Multiselect = false;

            // Call the ShowDialog method to show the dialog box.
            DialogResult userClickedOK = importFileDialog.ShowDialog();

            // Process input if the user clicked OK.
            if (userClickedOK == DialogResult.OK)
            {
                // Open the selected file to read.
                textBox1.Text = importFileDialog.FileName;
                loadDataGrid(textBox1.Text);
            }

        }
        private void loadDataGrid(string path)
        {
            try
            {
                DtSet = FileHelper.getDataSetFromExcel(path);
                dataGridView1.DataSource = DtSet.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
